package com.guigu.ssyx.activity.service.impl;

import com.atguigu.ssyx.enums.CouponRangeType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guigu.ssyx.activity.entity.CouponInfo;
import com.guigu.ssyx.activity.entity.CouponRange;
import com.guigu.ssyx.activity.mapper.CouponInfoMapper;
import com.guigu.ssyx.activity.mapper.CouponRangeMapper;
import com.guigu.ssyx.activity.service.CouponInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券信息 服务实现类
 * </p>
 *
 * @author liuyang
 * @since 2025-09-28
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {

    @Autowired
    private CouponRangeMapper couponRangeMapper;

    /**
     * 分页查询优惠券
     * @param couponInfoPage
     * @return
     */
    @Override
    public IPage<CouponInfo> selectPageCouponInfo(IPage<CouponInfo> couponInfoPage) {
        QueryWrapper<CouponInfo> couponInfoQueryWrapper = new QueryWrapper<>();
        couponInfoQueryWrapper.orderByDesc("id");

        IPage<CouponInfo> couponInfoIPage = baseMapper.selectPage(couponInfoPage, couponInfoQueryWrapper);
        couponInfoIPage.getRecords().stream().forEach(item->{
            item.setCouponTypeString(item.getCouponType().getComment());
            if(null != item.getRangeType()){
                item.setRangeTypeString(item.getRangeType().getComment());
            }
        });

        return couponInfoIPage;
    }

    @Override
    public Map<String,Object> findCouponRuleList(Long id) {
        Map<String,Object> result = new HashMap<>();
        CouponInfo couponInfo = baseMapper.selectById(id);
        QueryWrapper<CouponRange> couponRangeQueryWrapper = new QueryWrapper<>();
        couponRangeQueryWrapper.eq("coupon_id", id);
        List<CouponRange> couponRangeList = couponRangeMapper.selectList(couponRangeQueryWrapper);

        List<Long> rangeIdList = couponRangeList.stream().map(CouponRange::getRangeId).collect(Collectors.toList());

        if(!CollectionUtils.isEmpty(rangeIdList)){
            if(couponInfo.getRangeType() == CouponRangeType.SKU){
                //去调用Sku的服务查询sku的信息
                result.put("skuInfoList",null);
            }else if(couponInfo.getRangeType() == CouponRangeType.CATEGORY){
                //去调用Category的服务查询category的信息
                result.put("categoryList",null);
            }
        }

        return result;
    }
}
